﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EzDesk.DevHelper.HtmlToAspx
{
    public partial class MainForm : Form, IHtmlToAspxView
    {
        FormPresenter _presenter;

        public MainForm()
        {
            InitializeComponent();
            _presenter = new FormPresenter(this);
        }

        public void SetFormModel(FormModel model)
        {
            btnSyncHtml.Enabled = model.canSyncHtmlToAspx;
            btnSyncCss.Enabled = model.canSycnCssAndJs;
            ckbAutoCheckout.Enabled = model.canSycnCssAndJs && model.canSyncHtmlToAspx;

            txtSourceFolder.Text = model.SourceHtmlFileFolder;
            txtOutputFolder.Text = model.OutputFolder;

            listBox1.DataSource = model.SourceHtmlFileList;
            ckbAutoCheckout.Checked = model.isAutoCheckOut;
        }

        public void DisplayStatus(string status, bool isError)
        {
            listBox2.Items.Add(string.Format(
                "[{0}] - {1}",
                DateTime.Now,
                status
                ));
            // 自动滚动
            listBox2.SelectedItem = listBox2.Items[listBox2.Items.Count - 1];

            if (isError)
            {
                MessageBox.Show(status);
            }
        }



        private void btnAutoChooseSourceFolder_Click(object sender, EventArgs e)
        {
            if (_presenter != null) _presenter.AutoFindSourceFolder();
        }

        private void btnAutoChooseOutputFolder_Click(object sender, EventArgs e)
        {
            if (_presenter != null) _presenter.AutoOutputFolder();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            var fileNameList = listBox1.SelectedItems;

            foreach (var fileName in fileNameList)
            {
                if (_presenter != null)
                {
                    _presenter.ConvertHtmlToAspx(fileName as string);
                }   
            }
        }

        private void ckbAutoCheckout_CheckedChanged(object sender, EventArgs e)
        {
            if (_presenter != null)
                _presenter.SetAutoCheckOut(ckbAutoCheckout.Checked);
        }

        private void btnSyncCss_Click(object sender, EventArgs e)
        {
            if (_presenter != null)
            {
                _presenter.SyncCssAndJs();
            }
        }
    }
}
